package com.ruibang.glass.material.service.impl.yqyb;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.common.constants.Constants;
import com.ruibang.glass.common.domain.camunda.BpmProcessInstanceCreate;
import com.ruibang.glass.common.domain.camunda.BpmProcessInstanceStatus;
import com.ruibang.glass.material.domain.req.ycl.ManageOutBoundReq;
import com.ruibang.glass.material.domain.req.ycl.ManageOutInReq;
import com.ruibang.glass.material.dto.OutAndInManagementDTO;
import com.ruibang.glass.material.entity.yqyb.MaterialYBYQManagementInventory;
import com.ruibang.glass.material.entity.yqyb.MaterialYBYQOutboundManagement;
import com.ruibang.glass.material.feign.CamundaApi;
import com.ruibang.glass.material.mapper.ybyq.GMaterialYBYQOutboundManagementMapper;
import com.ruibang.glass.material.service.impl.CommonService;
import com.ruibang.glass.material.service.yqyb.GMaterialYBYQOutboundManagementService;
import com.teaming.cloud.framework2.common.model.ResultBody;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import java.util.stream.Collectors;

/**
 * <p>
 * 出库管理表 服务实现类
 * </p>
 *
 * @author mameng
 * @since 2024-04-01
 */
@Service
public class GMaterialYBYQOutboundManagementServiceImpl extends ServiceImpl<GMaterialYBYQOutboundManagementMapper, MaterialYBYQOutboundManagement> implements GMaterialYBYQOutboundManagementService {
    @Autowired
    private CommonService commonService;

    @Autowired
    private CamundaApi camundaApi;
    /**
     * 流程列表查询
     * @param
     * @return
     */
    @Override
    public IPage<MaterialYBYQOutboundManagement> getPage(ManageOutBoundReq materialInventoryReq) {
        LambdaQueryWrapper<MaterialYBYQOutboundManagement> queryWrapper = Wrappers.<MaterialYBYQOutboundManagement>lambdaQuery();
        if (StringUtils.isNotBlank(materialInventoryReq.getOrderNumber())) {
            queryWrapper.like(MaterialYBYQOutboundManagement::getOrderNumber, materialInventoryReq.getOrderNumber());
        }
        if (StringUtils.isNotBlank(materialInventoryReq.getType())) {
            queryWrapper.like(MaterialYBYQOutboundManagement::getType, materialInventoryReq.getType());
        }
        if (StringUtils.isNotBlank(materialInventoryReq.getStatus())) {
            queryWrapper.like(MaterialYBYQOutboundManagement::getStatus, materialInventoryReq.getStatus());
        }
        if (StringUtils.isNotBlank(materialInventoryReq.getApproveStatus())) {
            queryWrapper.like(MaterialYBYQOutboundManagement::getApproveStatus, materialInventoryReq.getApproveStatus());
        }
        if (StringUtils.isNotBlank(materialInventoryReq.getPreparer())) {
            queryWrapper.like(MaterialYBYQOutboundManagement::getPreparer, materialInventoryReq.getPreparer());
        }
        if (StringUtils.isNotBlank(materialInventoryReq.getBatchNumber())) {
            queryWrapper.like(MaterialYBYQOutboundManagement::getBatchNumber, materialInventoryReq.getBatchNumber());
        }
        if (StringUtils.isNotBlank(materialInventoryReq.getItemName())) {
            queryWrapper.like(MaterialYBYQOutboundManagement::getItemName, materialInventoryReq.getItemName());
        }
        if (StringUtils.isNotBlank(materialInventoryReq.getBarcodeNumber())) {
            queryWrapper.like(MaterialYBYQOutboundManagement::getBarcodeNumber, materialInventoryReq.getBarcodeNumber());
        }
        if (materialInventoryReq.getFinishStartDate() != null) {
            queryWrapper.ge(MaterialYBYQOutboundManagement::getFinishDate, materialInventoryReq.getFinishStartDate());
        }
        if (materialInventoryReq.getFinishEndDate() != null) {
            queryWrapper.le(MaterialYBYQOutboundManagement::getFinishDate, materialInventoryReq.getFinishEndDate());
        }
        Page<MaterialYBYQOutboundManagement> materialWarehouseInfoPage = baseMapper.selectPage(new Page<>(materialInventoryReq.getPageNo(), materialInventoryReq.getPageSize()),
                queryWrapper.orderByDesc(MaterialYBYQOutboundManagement::getCreationTime)
        );
        if (CollectionUtils.isEmpty(materialWarehouseInfoPage.getRecords())) {
            return materialWarehouseInfoPage;
        }
        List<String> businessKeys = materialWarehouseInfoPage.getRecords().stream().map(MaterialYBYQOutboundManagement::getId).collect(Collectors.toList());
        Map<String, Integer> statusMap = null;
        Map<String, String> processMap = null;
        //获取业务流程状态
        ResultBody<List<BpmProcessInstanceStatus>> resultBody = camundaApi.queryProcessInstanceStatus(businessKeys);
        if (resultBody != null && CollectionUtils.isNotEmpty(resultBody.getData())) {
            statusMap = resultBody.getData().stream().collect(Collectors.toMap(BpmProcessInstanceStatus::getBusinessKey, BpmProcessInstanceStatus::getStatus, (s1, s2) -> s1));
            processMap = resultBody.getData().stream().collect(Collectors.toMap(BpmProcessInstanceStatus::getBusinessKey, BpmProcessInstanceStatus::getProcessInstanceId, (s1, s2) -> s1));
        }
        for (MaterialYBYQOutboundManagement qualityContact : materialWarehouseInfoPage.getRecords()) {
            if (statusMap != null) {
                qualityContact.setStatus(statusMap.get(qualityContact.getId()).toString());
            }
            if (processMap != null) {
                qualityContact.setProcessId(processMap.get(qualityContact.getId()));
            }
        }
        return materialWarehouseInfoPage;
    }


    @Override
    public IPage<OutAndInManagementDTO> getPage(ManageOutInReq manageOutInReq) {
        return baseMapper.getPage(new Page<>(manageOutInReq.hashCode(), manageOutInReq.getPageSize()), manageOutInReq);
    }
    /**
     *新建流程
     * @param manageOutInReq
     * @return
     */
    @Override
    public void saveMaterialManagementInventory(MaterialYBYQOutboundManagement materialYBYQOutboundManagement) {
        materialYBYQOutboundManagement.setSpStatus(Constants.ONE_STR);
        baseMapper.insert(materialYBYQOutboundManagement);
        BpmProcessInstanceCreate instanceCreate = new BpmProcessInstanceCreate();
        instanceCreate.setBusinessKey(materialYBYQOutboundManagement.getId());
        instanceCreate.setMenuId(materialYBYQOutboundManagement.getMenuId());
        //发起流程
        if (StringUtils.isNotBlank(materialYBYQOutboundManagement.getPreparerId())) {
            Map<String, Object> variables = new HashMap<>();
            variables.put(com.ruibang.glass.common.constants.Constants.PROCESS_INSTANCE_STARTER_USER_ID, materialYBYQOutboundManagement.getPreparerId());
            variables.put(com.ruibang.glass.common.constants.Constants.PROCESS_INSTANCE_BUSINESS_KEY, materialYBYQOutboundManagement.getId());
            variables.put(com.ruibang.glass.common.constants.Constants.PROCESS_INSTANCE_MENU_ID, materialYBYQOutboundManagement.getMenuId());
            instanceCreate.setVariables(variables);
        }
        commonService.startProcess(instanceCreate);
    }
}
